<template>
  <div class="madp-customer-data">
    <div class="madp-nums">
      <div class="item">
        <span>客户总数</span>
        <span>{{ contactData.contactTotal | formatNum }}</span>
      </div>
      <div class="line"></div>
      <div class="item">
        <span>行内客户数</span>
        <span>{{ contactData.contactInside | formatNum }}</span>
      </div>
      <div class="line"></div>
      <div class="item">
        <span>昨日联系客户数</span>
        <span>{{ contactData.contactEffectiveYesterday | formatNum }}</span>
      </div>
    </div>
    <div class="madp-title">
      <div class="name">
        <span>客户简报</span>
      </div>
      <el-select v-model="dataViewTime" placeholder="请选择" @change="changDataViewTime">
        <el-option label="昨日" value="0"> </el-option>
        <el-option label="近7日" value="1"> </el-option>
        <el-option label="近30日" value="2"> </el-option>
      </el-select>
    </div>
    <div class="bulletins">
      <div class="item" v-for="(item, idx) in bulletins" :key="idx">
        <span>{{ item.name }}</span>
        <span>{{ item.num | formatNum }}{{ item.props == 'repliedRate' ? '%' : '' }}</span>
      </div>
    </div>
    <div class="madp-title">
      <div class="name">
        <span>新增客户数</span>
      </div>
    </div>
    <div>
      <echart-line-com chartId="addCustomers" :isUpdate.sync="isAddUpdateChart" :xData="addXData" :seriesData="addSeriesData" />
      <div class="chooseTime">
        <span @click="changeAddCusTime(1)" :class="addCusTime == 1 ? 'is-active' : ''">近一周</span>
        <span class="line"></span>
        <span @click="changeAddCusTime(2)" :class="addCusTime == 2 ? 'is-active' : ''">近一月</span>
      </div>
    </div>
    <div class="madp-title">
      <div class="name">
        <span>行内客户数</span>
      </div>
    </div>
    <div>
      <echart-line-com chartId="bankCustomers" :isUpdate.sync="isBankUpdateChart" :xData="bankXData" :seriesData="bankSeriesData" />
      <div class="chooseTime">
        <span @click="changeBackCusTime(1)" :class="bankCusTime == 1 ? 'is-active' : ''">近一周</span>
        <span class="line"></span>
        <span @click="changeBackCusTime(2)" :class="bankCusTime == 2 ? 'is-active' : ''">近一月</span>
      </div>
    </div>
  </div>
</template>
<script>
import echartLineCom from '@/components/manager-view/enchart-line.vue'
import echartPieCom from '@/components/manager-view/enchart-pie.vue'
import managerViewApi from '@/api/manager-view.js'
export default {
  components: { echartLineCom, echartPieCom },
  data() {
    return {
      contactData: {}, //客户总数
      ontactBriefing: {}, // 客户简报
      isAddUpdateChart: false,
      isBankUpdateChart: false,
      dataViewTime: '0', // 数据总览时间周期
      addCusTime: 1, // 新增客户数周期
      bankCusTime: 1, // 行内客户数周期
      // 折线图显示数据
      addXData: [], // 新增客户数 X轴显示
      addSeriesData: [], // 新增客户数 数据显示
      bankXData: [], // 行内客户数 X轴显示
      bankSeriesData: [], // 行内客户数 数据显示
      // 客户简报
      bulletins: [
        {
          name: '新增客户数',
          props: 'contactAdd',
          num: 0
        },
        {
          name: '行内客户数',
          props: 'contactInside',
          num: 0
        },
        {
          name: '有效联系客户数',
          props: 'contactEffective',
          num: 0
        },
        {
          name: '发送消息数',
          props: 'messageSend',
          num: 0
        },
        {
          name: '已回复占比',
          props: 'repliedRate',
          num: 0
        },
        {
          name: '平均首次回复时长(分)',
          props: 'avgReplyTime',
          num: 0
        }
      ]
    }
  },
  created() {
    this.getContactData()
    this.getContactBriefing()
    this.getTendency(1)
    this.getTendency(2)
  },
  mounted() {},
  methods: {
    // 客户简报
    changDataViewTime() {
      this.getContactBriefing()
    },

    // 新增客户数周期变化
    changeAddCusTime(type) {
      this.addCusTime = type
      this.getTendency(1)
    },
    // 行内客户数周期变化
    changeBackCusTime(type) {
      this.bankCusTime = type
      this.getTendency(2)
    },
    // 客户总数
    getContactData() {
      managerViewApi
        .getContactData({
          wxUserId: this.$store.state.vuex_wxUserId
        })
        .then((res) => {
          this.contactData = res.data.data
        })
    },
    // 客户简报
    getContactBriefing() {
      managerViewApi
        .getContactBriefing({
          wxUserId: this.$store.state.vuex_wxUserId,
          dateType: this.dataViewTime
        })
        .then((res) => {
          let info = res.data.data
          this.bulletins.forEach((ele) => {
            ele.num = info[ele.props] || 0
            ele.num = ele.num - 0
          })
        })
    },
    // 趋势图 type==1 新增客户数 type==2否则查行内客户数
    getTendency(type) {
      managerViewApi
        .getTendency({
          wxUserId: this.$store.state.vuex_wxUserId,
          dataType: type,
          dateType: type == 1 ? this.addCusTime : this.bankCusTime
        })
        .then((res) => {
          let list = res.data.data
          let { xData, seriesData } = this.getEchartData(list)
          if (type == 1) {
            this.addXData = xData
            this.addSeriesData = seriesData
            this.isAddUpdateChart = true
          } else {
            this.bankXData = xData
            this.bankSeriesData = seriesData
            this.isBankUpdateChart = true
          }
        })
    },
    // 获取echart 需要的数据结构
    getEchartData(list) {
      let xData = []
      let seriesData = []
      list.forEach((ele, idx) => {
        if (list.length > 7 && (idx === 0 || idx === list.length - 1)) {
          xData.push(ele.time)
        } else if (list.length <= 7) {
          let time = ele.time.split('-')
          xData.push(`${time[1]}/${time[2]}`)
        } else {
          xData.push('')
        }
        seriesData.push(ele.number)
      })
      return { xData, seriesData }
    }
  }
}
</script>
<style scoped lang='scss'>
.madp-customer-data {
  .madp-nums {
    margin-top: 16px;
    width: 100%;
    background-image: url('../../assets/img/managerView/shujvkanban_bg.png');
    background-repeat: no-repeat;
    background-size: cover;
    display: flex;
    justify-content: space-evenly;
    padding-top: 18px;
    padding-bottom: 15px;
    box-shadow: 0px 3px 6px 0px rgba(181, 181, 181, 0.16);
    .line {
      width: 1px;
      height: 53px;
      border: 1px solid rgba(44, 44, 44, 0.1);
    }
    .item {
      display: flex;
      flex-direction: column;
      align-items: center;
      :nth-child(1) {
        font-size: 12px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: rgba(32, 32, 32, 0.5);
      }
      :nth-child(2) {
        font-size: 24px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #202020;
      }
    }
  }
  .madp-title {
    margin-top: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .name {
      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #2c2c2c;
      :nth-child(2) {
        color: rgba(44, 44, 44, 0.5);
      }
    }
    .el-select {
      width: 85px;
      /deep/.el-input__inner {
        height: 30px;
        line-height: 30px;
        border-radius: 13px;
        border: 1px solid rgba(44, 44, 44, 0.1);
        font-size: 12px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #2c2c2c;
      }
      /deep/.el-input__icon {
        line-height: 1;
      }
    }
    .el-input {
      width: 120px;
      margin-left: 50px;
      /deep/.el-input__inner {
        height: 30px;
        line-height: 30px;
        border-radius: 13px;
        border: 1px solid rgba(44, 44, 44, 0.1);
        font-size: 12px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #2c2c2c;
      }
      /deep/.el-input__icon {
        line-height: 1;
      }
    }
  }
  .bulletins {
    display: flex;
    flex-wrap: wrap;
    .item {
      width: 109px;
      height: 85px;
      background: #fafafa;
      border-radius: 8px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      margin-top: 10px;
      margin-right: 8px;
      :nth-child(1) {
        font-size: 12px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: rgba(44, 44, 44, 0.8);
        text-align: center;
      }
      :nth-child(2) {
        margin-top: 10px;
        font-size: 20px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #2c2c2c;
      }
    }
    :nth-child(3n) {
      margin-right: 0;
    }
  }
  .chooseTime {
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 16px 0;
    span {
      font-size: 10px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: rgba(44, 44, 44, 0.5);
      padding: 3px 15px;
    }
    .line {
      padding: 0;
      width: 1px;
      height: 12px;
      background: rgba(44, 44, 44, 0.1);
    }
    .is-active {
      border-radius: 10px;
      background: rgba(252, 130, 63, 0.2);
      color: #fc823f;
    }
  }
}
</style>